import {
    call, // 调用
    put, // 提交
    takeEvery // 监听
} from 'redux-saga/effects'
import { AxiosResponse } from 'axios'
import axios from 'axios'

const getTodos =  () => {
    return  axios.get('https://jsonplaceholder.typicode.com/todos')
}

function* getTodosSaga() {
    try {
        const res: AxiosResponse = yield call(getTodos)
        yield put({
            type: 'todos/fetchTodosSuccess', // action
            payload: res.data // 请求结果
        })
    } catch (error) {
        yield put({ type: 'todos/fetchTodosFailure', payload: error })
    }
}
export default getTodosSaga

/**
 * 上层监听
 * disptach 时优先到 takeEvery 然后执行 getTodosSaga
 * getTodosSaga 中再 disptach action ~~ todos/fetchTodosSuccess
 */
export const todosSagas = [
    takeEvery('todos/fetchTodos', getTodosSaga)
    // ... 多个请求
]
